


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*; 
import java.io.*;

public class PolicyPropertyFrame extends JPanel implements ActionListener, 
DocumentListener, ItemListener,WindowListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6271033267436565657L;

	//data
	PolicyProperty pp;
	
	JTabbedPane tab;
	
	JPanel or;
	JPanel we;
	JPanel ac;
	JPanel as;
	JPanel ex;
	
	//organization page
	JTextField orgName;
	JTextField email;
	JTextField webpage;
	JTextField telephone;
	JTextField mailingName;
	JTextArea streetAddress;
	JTextField city;
	JTextField state;
	JTextField postal;
	JTextField country;
	
	//website page
	JTextField policyName;
	JTextField optUrl;
	JComboBox language;
	JTextField readablePolicy;
	
	//access page
	JRadioButton noneCollect;
	JRadioButton all;
	JRadioButton conAndOth;
	JRadioButton contact;
	JRadioButton other;
	JRadioButton none;
	
	//assurance page
	JList list;
	JButton add;
	JButton remove;
	JButton modify;
	
	//page expire
	JRadioButton byLifetime;
	JRadioButton byDate;
	JTextField lifetime;
	JComboBox units;
	JComboBox exD;
	JComboBox exM;
	JComboBox exY;
	JCheckBox testOnly;
	
	//Jdialog to create/modify an assurance
	JDialog dialog;
	JTabbedPane aTab;
	JTextField aName;
	JTextField aUrl;
	JRadioButton aCustSvr;
	JRadioButton aIndpOrg;
	JRadioButton aCourt;
	JRadioButton aApblLaw;
	JTextArea aDesp;
	JCheckBox errCorrect;
	JCheckBox lawSpecifiy;
	JCheckBox monetaryPay;
	JButton aOk;
	JButton aCancel;
	//modifyIndex indicates which seal is being edited, if<0 then it's a new one
	int modifyIndex;
	
	JDialog noName;
	JButton noNameOk;

	/************************************/
	public PolicyPropertyFrame(){
		super(new BorderLayout());
		pp=new PolicyProperty();
		modifyIndex=-1;
	}
	
	public void config(){
		
		tab=new JTabbedPane();
		
		//organization page
		orgName=new JTextField(30);
		email=new JTextField(30);
		webpage=new JTextField(30);
		telephone=new JTextField(30);
		mailingName=new JTextField(30);
		streetAddress=new JTextArea(6,29);
		streetAddress.setLineWrap(true);
		JScrollPane sp=new JScrollPane(streetAddress);
		sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		city=new JTextField(30);
		state=new JTextField(30);
		postal=new JTextField(30);
		country=new JTextField(30);

		JLabel orgNameL=new JLabel("organization name");
		JLabel emailL=new JLabel("email address");
		JLabel webpageL=new JLabel("web homepage");
		JLabel telephoneL=new JLabel("telephone number");
		JLabel mailingNameL=new JLabel("name");
		JLabel streetAddressL=new JLabel("       street address");
		JLabel cityL=new JLabel("city");
		JLabel stateL=new JLabel("state/province");
		JLabel postalL=new JLabel("postal/ZIP code");
		JLabel countryL=new JLabel("country");
		JLabel infoL=new JLabel("information about the organization collecting data");
		JLabel addrL=new JLabel("mailing address");
		
		JPanel line1=new JPanel();
		JPanel line2=new JPanel();
		JPanel line3=new JPanel();
		JPanel line4=new JPanel();
		JPanel line5=new JPanel();
		JPanel line6=new JPanel();
		JPanel line7=new JPanel();
		JPanel line8_1=new JPanel();
		JPanel line8=new JPanel();
		JPanel line9=new JPanel();
		JPanel line10=new JPanel();
		JPanel line11=new JPanel();
		JPanel line12=new JPanel();
		
		line1.add(infoL);
		line2.setLayout(new FlowLayout(FlowLayout.RIGHT));
		line2.add(orgNameL);
		line2.add(orgName);
		line3.setLayout(new FlowLayout(FlowLayout.RIGHT));
		line3.add(emailL);
		line3.add(email);
		line4.setLayout(new FlowLayout(FlowLayout.RIGHT));
		line4.add(webpageL);
		line4.add(webpage);
		line5.setLayout(new FlowLayout(FlowLayout.RIGHT));
		line5.add(telephoneL);
		line5.add(telephone);
		line6.add(addrL);
		line7.setLayout(new FlowLayout(FlowLayout.RIGHT));
		line7.add(mailingNameL);
		line7.add(mailingName);
		line8_1.setLayout(new FlowLayout(FlowLayout.LEFT));
		line8_1.add(streetAddressL);
		line8.setLayout(new FlowLayout(FlowLayout.RIGHT));
		line8.add(sp);
		line9.setLayout(new FlowLayout(FlowLayout.RIGHT));
		line9.add(cityL);
		line9.add(city);
		line10.setLayout(new FlowLayout(FlowLayout.RIGHT));
		line10.add(stateL);
		line10.add(state);
		line11.setLayout(new FlowLayout(FlowLayout.RIGHT));
		line11.add(postalL);
		line11.add(postal);
		line12.setLayout(new FlowLayout(FlowLayout.RIGHT));
		line12.add(countryL);
		line12.add(country);
		
		JPanel l1=new JPanel();
		l1.setLayout(new BorderLayout());
		JPanel l2=new JPanel();
		l2.setLayout(new BorderLayout());
		JPanel l3=new JPanel();
		l3.setLayout(new BorderLayout());
		JPanel l4=new JPanel();
		l4.setLayout(new BorderLayout());
		JPanel l5=new JPanel();
		l5.setLayout(new BorderLayout());
		JPanel l6=new JPanel();
		l6.setLayout(new BorderLayout());
		JPanel l7=new JPanel();
		l7.setLayout(new BorderLayout());
		JPanel l8=new JPanel();
		l8.setLayout(new BorderLayout());
		JPanel l9=new JPanel();
		l9.setLayout(new BorderLayout());
		JPanel l10=new JPanel();
		l10.setLayout(new BorderLayout());
		JPanel l11=new JPanel();
		l11.setLayout(new BorderLayout());
		JPanel l12=new JPanel();
		l12.setLayout(new BorderLayout());
		JPanel l13=new JPanel();
		l13.setLayout(new BorderLayout());
		
		l1.add(line8,"Center");
		l1.add(line8_1,"North");
		l2.add(l1,"Center");
		l2.add(line7,"North");
		l3.add(l2,"Center");
		l3.add(line6,"North");
		l4.add(l3,"Center");
		l4.add(line6,"North");		
		l5.add(l4,"Center");
		l5.add(line5,"North");		
		l6.add(l5,"Center");
		l6.add(line4,"North");		
		l7.add(l6,"Center");
		l7.add(line3,"North");		
		l8.add(l7,"Center");
		l8.add(line2,"North");		
		l9.add(l8,"Center");
		l9.add(line1,"North");		
		l10.add(l9,"North");
		l10.add(line9,"Center");		
		l11.add(l10,"North");
		l11.add(line10,"Center");
		l12.add(l11,"North");
		l12.add(line11,"Center");
		l13.add(l12,"North");
		l13.add(line12,"Center");
		
		or=new JPanel();
		or.setLayout(new FlowLayout(FlowLayout.CENTER));
		or.add(l13);
		
		tab.add("organization",or);
		
		//websites page
		policyName = new JTextField(30);
		optUrl=new JTextField(30);
		String languages[]={"Chinese","English","French","German",
				"Greek","Italian","Japanese","Klingon","Korean",
				"Portugese","Russian","Spanish"};
		language=new JComboBox(languages);
		language.setSelectedItem(languages[1]);
		readablePolicy=new JTextField(30);
		
		JLabel policyNameL=new JLabel("policy name");
		JLabel optUrlL=new JLabel("opt-in/opt-out URL");
		JLabel languageL=new JLabel("                          policy language ");
		JLabel readablePolicyL=new JLabel("URL of human readable policy");
		
		JPanel pan1=new JPanel();
		JPanel pan2=new JPanel();
		JPanel pan3=new JPanel();
		JPanel pan4=new JPanel();
		pan1.setLayout(new FlowLayout(FlowLayout.RIGHT));
		pan2.setLayout(new FlowLayout(FlowLayout.RIGHT));
		pan3.setLayout(new FlowLayout(FlowLayout.LEFT));
		pan4.setLayout(new FlowLayout(FlowLayout.RIGHT));
		pan1.add(policyNameL);
		pan1.add(policyName);
		pan2.add(optUrlL);
		pan2.add(optUrl);
		pan3.add(languageL);
		pan3.add(language);
		pan4.add(readablePolicyL);
		pan4.add(readablePolicy);
		
		JPanel pan5=new JPanel();
		JPanel pan6=new JPanel();
		JPanel pan7=new JPanel();
		pan5.setLayout(new BorderLayout());
		pan6.setLayout(new BorderLayout());
		pan7.setLayout(new BorderLayout());
		pan5.add(pan3,"Center");
		pan5.add(pan4,"South");
		pan6.add(pan2,"North");
		pan6.add(pan5,"Center");
		pan7.add(pan1,"North");
		pan7.add(pan6,"Center");
		
		we=new JPanel();
		we.setLayout(new FlowLayout(FlowLayout.CENTER));
		we.add(pan7);
		
		tab.add("web sites",we);
		
		//access page
		noneCollect = new JRadioButton("No identified data is collected(so no access is possible)");
		all = new JRadioButton("All information on the user");
		conAndOth = new JRadioButton("Contact and other information");
		contact = new JRadioButton("Contact information only");
		other = new JRadioButton("Other information");
		none = new JRadioButton("Node(User cannot view their information)");
		
		JPanel rbtns=new JPanel();
		rbtns.setLayout(new GridLayout(6,1));
		rbtns.add(noneCollect);
		rbtns.add(all);
		rbtns.add(conAndOth);
		rbtns.add(contact);
		rbtns.add(other);
		rbtns.add(none);
		ButtonGroup group=new ButtonGroup();
		group.add(noneCollect);
		group.add(all);
		group.add(conAndOth);
		group.add(contact);
		group.add(other);
		group.add(none);
		none.setSelected(true);
		String statement1="<html> <body> <br><br>For sites which collect data associated with an individual user, " +
				"<br> indicate what data the user is allowed to view or update:<br><br> </body> </html> ";
		JLabel st1=new JLabel(statement1);
		
		String statement2="<html> <body> <br>The site's human-readable privacy policy should contain instructions on" +
				"<br> how users can view or uodate the information the site has collected on them.</body> </html> ";
		JLabel st2=new JLabel(statement2);
		
		JPanel ac1=new JPanel();
		JPanel ac2=new JPanel();
		JPanel ac3=new JPanel();
		ac1.setLayout(new BorderLayout());
		ac2.setLayout(new BorderLayout());
		ac3.setLayout(new BorderLayout());
		ac1.add(rbtns,"North");
		ac1.add(st2,"Center");
		ac2.add(ac1,"Center");
		ac2.add(st1,"North");
		ac3.add(ac2,"North");
		JPanel ac4=new JPanel();
		ac4.setLayout(new FlowLayout(FlowLayout.CENTER));
		ac4.add(ac3);
		
		ac=new JPanel();
		ac.setLayout(new BorderLayout());
		ac.add(ac4,"Center");
		
		tab.add("access page",ac);
		
		//assurance page
		list=new JList();
		list.setListData(pp.assurance);
		list.setFixedCellWidth(300);
		JScrollPane sp2=new JScrollPane(list);
		sp2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		sp2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		add=new JButton("Add");
		remove=new JButton("Remove");
		modify=new JButton("Modify");
		remove.setEnabled(false);
		modify.setEnabled(false);
		
		JLabel st4=new JLabel("<html><body>list the \"privacy seals\" or privacy legislation covering this policy<br><br></body></html>");
		
		JPanel as1=new JPanel();
		JPanel as2=new JPanel();
		JPanel as3=new JPanel();
		
		as1.setLayout(new GridLayout(3,1));
		as1.add(add);
		as1.add(remove);
		as1.add(modify);
		as2.setLayout(new FlowLayout(FlowLayout.CENTER));
		as2.add(sp2);
		as2.add(as1);
		as3.setLayout(new BorderLayout());
		as3.add(st4,"North");
		as3.add(as2,"Center");
		
		as = new JPanel();
		as.setLayout(new FlowLayout(FlowLayout.CENTER));
		as.add(as3);
		
		tab.add("Assurances",as);
		//page expire
		byLifetime =new JRadioButton("relative lifetime");
		byDate =new JRadioButton("expiry date        ");
		ButtonGroup group1=new ButtonGroup();
		group1.add(byLifetime);
		group1.add(byDate);
		byLifetime.setSelected(true);
		lifetime=new JTextField(5);
		lifetime.setText("1");
		
		String timeUnit[]={"Second(s)","Minute(s)","Hour(s)","Day(s)","Week(s)"};
		units=new JComboBox(timeUnit);
		units.setSelectedIndex(3);
		
		String exDs[]={"01","02","03","04","05","06","07","08","09","10",
				"11","12","13","14","15","16","17","18","19","20",
				"21","22","23","24","25","26","27","28","29","30","31"};
		exD=new JComboBox(exDs);
		exD.setSelectedIndex(0);
		
		String exMs[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
		exM=new JComboBox(exMs);
		exM.setSelectedIndex(0);
		
		String exYs[]={"2010","2011","2012","2013","2014","2015","2016","2017","2018","2019","2020"};
		exY=new JComboBox(exYs);
		exY.setSelectedIndex(0);
		
		exD.setEnabled(false);
		exM.setEnabled(false);
		exY.setEnabled(false);
		
		testOnly=new JCheckBox("policy is for test purpose only");
		testOnly.setSelected(false);
		
		JLabel st3=new JLabel("<html><body>Indicate how long this policy will be valid<br><br></body></html>");
		
		JPanel ex1=new JPanel();
		JPanel ex2=new JPanel();
		JPanel ex3=new JPanel();
		JPanel ex4=new JPanel();
		JPanel ex5=new JPanel();
		JPanel ex6=new JPanel();
		ex1.setLayout(new FlowLayout(FlowLayout.LEFT));
		ex2.setLayout(new FlowLayout(FlowLayout.LEFT));
		ex3.setLayout(new BorderLayout());
		ex4.setLayout(new BorderLayout());
		ex5.setLayout(new BorderLayout());
		ex6.setLayout(new FlowLayout(FlowLayout.CENTER));
		ex1.add(byLifetime);
		ex1.add(lifetime);
		ex1.add(units);
		ex2.add(byDate);
		ex2.add(exD);
		ex2.add(exM);
		ex2.add(exY);
		ex3.add(st3,"North");
		ex3.add(ex1,"Center");
		ex4.add(ex3,"North");
		ex4.add(ex2,"Center");
		ex5.add(ex4,"North");
		ex5.add(testOnly,"Center");
		ex6.add(ex5);
		ex=new JPanel();
		ex.setLayout(new BorderLayout());
		ex.add(ex6,"North");
		tab.add("expiry",ex);
		
		/*****************ADD LISTENER*****************/
		orgName.getDocument().addDocumentListener(this);
		email.getDocument().addDocumentListener(this);
		webpage.getDocument().addDocumentListener(this);
		telephone.getDocument().addDocumentListener(this);
		mailingName.getDocument().addDocumentListener(this);
		streetAddress.getDocument().addDocumentListener(this);
		city.getDocument().addDocumentListener(this);
		state.getDocument().addDocumentListener(this);
		postal.getDocument().addDocumentListener(this);
		country.getDocument().addDocumentListener(this);
		
		policyName.getDocument().addDocumentListener(this);
		optUrl.getDocument().addDocumentListener(this);
		readablePolicy.getDocument().addDocumentListener(this);
		language.addItemListener(this);
		
		noneCollect.addActionListener(this);
		all.addActionListener(this);
		conAndOth.addActionListener(this);
		contact.addActionListener(this);
		other.addActionListener(this);
		none.addActionListener(this);
		
		add.addActionListener(this);
		remove.addActionListener(this);
		modify.addActionListener(this);
		
		byLifetime.addActionListener(this);
		byDate.addActionListener(this);
		lifetime.getDocument().addDocumentListener(this);
		units.addItemListener(this);
		exD.addItemListener(this);
		exM.addItemListener(this);
		exY.addItemListener(this);
		testOnly.addItemListener(this);
		/*****************LISTENER*****************/

		add(tab,"Center");

		initDialog();
	}
	
	public void initDialog(){
		/*********dialog for creating assurances***/
		dialog=new JDialog(MainFrame.getInstance(),"new dispute property");
		dialog.setSize(400,450);
		dialog.setResizable(false);
		aName=new JTextField(30);
		aUrl=new JTextField(30);
		
		aCustSvr=new JRadioButton("customer service");
		aIndpOrg=new JRadioButton("independent organization");
		aCourt=new JRadioButton("court");
		aApblLaw=new JRadioButton("applicable law");
		ButtonGroup group2=new ButtonGroup();
		group2.add(aCustSvr);
		group2.add(aIndpOrg);
		group2.add(aCourt);
		group2.add(aApblLaw);
		aCustSvr.setSelected(true);
		aDesp=new JTextArea(6,29);
		aDesp.setLineWrap(true);
		JScrollPane sp3=new JScrollPane(aDesp);
		sp3.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		sp3.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		errCorrect=new JCheckBox("the error will be corrected");
		lawSpecifiy=new JCheckBox("governing law specifies remedies");
		monetaryPay=new JCheckBox("monetary damage will be paid to the user");
		aOk=new JButton("OK");
		aCancel=new JButton("Cancel");
		
		aOk.addActionListener(this);
		aCancel.addActionListener(this);
		
		JLabel aNameL=new JLabel("name");
		JLabel aUrlL=new JLabel("URL");
		JLabel aTypeL=new JLabel("type");
		JLabel aDespL=new JLabel("description");
		JLabel aSt1L=new JLabel("<html><body><br> Select the remedies (if any) applicable to users <br> in the event of a breach od this policy<br><br></body></html>");
		JLabel aSt2L=new JLabel("<html><body><br> Your human-readable privacy policy should explain <br> how remedies will be given to the users.</body></html>");
		
		aTab=new JTabbedPane();
		
		JPanel aA1=new JPanel();
		aA1.setLayout(new FlowLayout(FlowLayout.RIGHT));
		aA1.add(aNameL);
		aA1.add(aName);
		JPanel aA2=new JPanel();
		aA2.setLayout(new FlowLayout(FlowLayout.RIGHT));
		aA2.add(aUrlL);
		aA2.add(aUrl);
		JPanel aA3=new JPanel();
		aA3.setLayout(new GridLayout(4,1));
		aA3.add(aCustSvr);
		aA3.add(aIndpOrg);
		aA3.add(aCourt);
		aA3.add(aApblLaw);
		JPanel aA4=new JPanel();
		aA4.setLayout(new BorderLayout());
		aA4.add(aA3,"Center");
		aA4.add(aTypeL,"North");
		JPanel aA5=new JPanel();
		aA5.setLayout(new FlowLayout(FlowLayout.CENTER));
		aA5.add(aA4);
		JPanel aA6=new JPanel();
		aA6.setLayout(new BorderLayout());
		aA6.add(sp3,"Center");
		aA6.add(aDespL,"North");
		JPanel aA7=new JPanel();
		aA7.setLayout(new FlowLayout(FlowLayout.CENTER));
		aA7.add(aA6);
		JPanel aA8=new JPanel();
		aA8.setLayout(new BorderLayout());
		JPanel aA9=new JPanel();
		aA9.setLayout(new BorderLayout());
		JPanel aA=new JPanel();
		aA.setLayout(new BorderLayout());
		JPanel aA11=new JPanel();
		aA11.setLayout(new BorderLayout());
		aA8.add(aA7,"Center");
		aA8.add(aA5,"North");
		aA9.add(aA8,"Center");
		aA9.add(aA2,"North");
		aA.add(aA9,"Center");
		aA.add(aA1,"North");
				
		aTab.add("general",aA);
		
		JPanel aB1 = new JPanel();
		aB1.setLayout(new GridLayout(3,1));
		aB1.add(errCorrect);
		aB1.add(lawSpecifiy);
		aB1.add(monetaryPay);
		JPanel aB2 = new JPanel();
		aB2.setLayout(new FlowLayout(FlowLayout.CENTER));
		aB2.add(aB1);
		JPanel aB3=new JPanel();
		aB3.setLayout(new BorderLayout());
		JPanel aB4=new JPanel();
		aB4.setLayout(new BorderLayout());
		aB3.add(aB2,"Center");
		aB3.add(aSt1L,"North");
		aB4.add(aB3,"North");
		aB4.add(aSt2L,"Center");
		JPanel aB = new JPanel();
		aB.setLayout(new FlowLayout(FlowLayout.CENTER));
		aB.add(aB4);
		
		aTab.add("remedies",aB);
		
		JPanel abuttons=new JPanel();
		abuttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		abuttons.add(aOk);
		abuttons.add(aCancel);
		dialog.setLayout(new BorderLayout());
		dialog.add(aTab,"Center");
		dialog.add(abuttons,"South");
		
		/**********init warning dialog******************/
		noName=new JDialog(dialog,"Warning");
		noName.setSize(200,100);
		noName.setResizable(false);
		noNameOk=new JButton("Ok");
		noNameOk.addActionListener(this);
		JLabel noNameWarn=new JLabel("You must specify a name");
		JPanel no1=new JPanel(new FlowLayout(FlowLayout.CENTER));
		JPanel no2=new JPanel(new FlowLayout(FlowLayout.CENTER));
		no1.add(noNameWarn);
		no2.add(noNameOk);
		JPanel noPan=new JPanel();
		noPan.setLayout(new BorderLayout());
		noPan.add(no1,"Center");
		noPan.add(no2,"South");
		noName.add(noPan);
		
		/********** set dialog location****************/
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension d=toolkit.getScreenSize();
		dialog.setLocation((d.width-400)/2,(d.height-450)/2);
		dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		dialog.addWindowListener(this);
		noName.setLocation((d.width-200)/2,(d.height-100)/2);
	}
	
	public void clearDialog(){
		aName.setText("");
		aUrl.setText("");
		aCustSvr.setSelected(true);
		aDesp.setText("");
		errCorrect.setSelected(false);
		lawSpecifiy.setSelected(false);
		monetaryPay.setSelected(false);
		aTab.setSelectedIndex(0);
	}
	
	public void loadDialog(int i){
		Assurance a=pp.getAssurance().get(i);
		aName.setText(a.getName());
		aUrl.setText(a.getUrl());

		if(a.getType()==Assurance.TYPE_CUSTOMER_SERVICE)
			aCustSvr.setSelected(true);
		if(a.getType()==Assurance.TYPE_INDEPENDENT_ORGANIZATION)
			aIndpOrg.setSelected(true);
		if(a.getType()==Assurance.TYPE_COURT)
			aCourt.setSelected(true);
		if(a.getType()==Assurance.TYPE_APPLICABLE_LAW)
			aApblLaw.setSelected(true);

		aDesp.setText(a.getDescription());
		errCorrect.setSelected(a.isErrorCorrected());
		lawSpecifiy.setSelected(a.isLawSpecifies());
		monetaryPay.setSelected(a.isMonetaryPaid());
		aTab.setSelectedIndex(0);
	}
	
	public void contentChanged(){
		int index=MainFrame.getInstance().tabPan.getSelectedIndex();
		PolicyFrame pf=MainFrame.getInstance().policies.get(index);
		if(!pf.changed){
			pf.changed=true;
			String title=pf.projectName;
			if(title==""){
				title=MainFrame.getInstance().tabPan.getTitleAt(index);
			}
			title="*"+title;
			MainFrame.getInstance().tabPan.setTitleAt(index, title);
		}
	}
	
	
	
	@Override
	public void changedUpdate(DocumentEvent arg0) {
		// TODO Auto-generated method stub
		contentChanged();
	}
	@Override
	public void insertUpdate(DocumentEvent arg0) {
		// TODO Auto-generated method stub
		contentChanged();
	}
	@Override
	public void removeUpdate(DocumentEvent arg0) {
		// TODO Auto-generated method stub
		contentChanged();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource().equals(noneCollect)||e.getSource().equals(all)||e.getSource().equals(conAndOth)
				||e.getSource().equals(contact)||e.getSource().equals(other)||e.getSource().equals(none)){
			contentChanged();
		}

		/** this is to switch expiry between by lifetime and by date*/
		if(e.getSource().equals(byLifetime)||e.getSource().equals(byDate)){
			contentChanged();
			if(byLifetime.isSelected()){
				units.setEnabled(true);
				lifetime.setEnabled(true);
				exD.setEnabled(false);
				exM.setEnabled(false);
				exY.setEnabled(false);
			}
			if(byDate.isSelected()){
				units.setEnabled(false);
				lifetime.setEnabled(false);
				exD.setEnabled(true);
				exM.setEnabled(true);
				exY.setEnabled(true);
			}
		}
		
		/**************deal with assurance modification**********/
		// add an assurance
		if(e.getSource().equals(add)){
			clearDialog();
			modifyIndex=-1;
//			dialog.show();
			MainFrame.getInstance().tabPan.setEnabled(false);
			int s=MainFrame.getInstance().tabPan.getSelectedIndex();
			PolicyFrame pf= MainFrame.getInstance().policies.get(s);
			pf.property.tab.setEnabled(false);
			pf.tab.setEnabled(false);
			dialog.setVisible(true);
		}
		
		// modify an assurance
		if(e.getSource().equals(modify)){
			int i=list.getSelectedIndex();
			if(i>=0){
				modifyIndex=list.getSelectedIndex();
				loadDialog(modifyIndex);
//				dialog.show();
				MainFrame.getInstance().tabPan.setEnabled(false);
				int s=MainFrame.getInstance().tabPan.getSelectedIndex();
				PolicyFrame pf= MainFrame.getInstance().policies.get(s);
				pf.property.tab.setEnabled(false);
				pf.tab.setEnabled(false);
				dialog.setVisible(true);
			}
		}
		
		// remove an assurance
		if(e.getSource().equals(remove)){
			int i=list.getSelectedIndex();
			if(i>=0){
				pp.getAssurance().remove(i);
				list.setListData(pp.getAssurance());
				list.repaint();
				contentChanged();
				if(pp.getAssurance().size()==0){
					remove.setEnabled(false);
					modify.setEnabled(false);
				}
			}
		}
		
		// cancel editing an assurance
		if(e.getSource().equals(aCancel)){
			int s=MainFrame.getInstance().tabPan.getSelectedIndex();
			PolicyFrame pf= MainFrame.getInstance().policies.get(s);
			pf.property.tab.setEnabled(true);
			pf.tab.setEnabled(true);
			MainFrame.getInstance().tabPan.setEnabled(true);
			dialog.dispose();
		}
		
		// save an assurance
		if(e.getSource().equals(aOk)){
			if(aName.getText().replaceAll(" ", "").equals("")){
//				noName.show();
				noName.setVisible(true);
			}else{
				String name= aName.getText();
				String url= aUrl.getText();
				int type=0;
				if(aCustSvr.isSelected()){
					type=Assurance.TYPE_CUSTOMER_SERVICE;
				}
				if(aIndpOrg.isSelected()){
					type=Assurance.TYPE_INDEPENDENT_ORGANIZATION;
				}
				if(aCourt.isSelected()){
					type=Assurance.TYPE_COURT;
				}
				if(aApblLaw.isSelected()){
					type=Assurance.TYPE_APPLICABLE_LAW;
				}
				String descrip=aDesp.getText();
				boolean eC=errCorrect.isSelected();
				boolean lS=lawSpecifiy.isSelected();
				boolean mP=monetaryPay.isSelected();
				Assurance res=new Assurance(name,url,type,descrip,eC,lS,mP);
				if(modifyIndex<0){
					pp.getAssurance().add(res);
					list.setListData(pp.getAssurance());
					list.repaint();
				}else{
					if(modifyIndex<pp.getAssurance().size()){
						pp.getAssurance().set(modifyIndex, res);
						list.setListData(pp.getAssurance());
						list.repaint();
					}
				}
				if(pp.getAssurance().size()>0){
					remove.setEnabled(true);
					modify.setEnabled(true);
				}
				int s=MainFrame.getInstance().tabPan.getSelectedIndex();
				PolicyFrame pf= MainFrame.getInstance().policies.get(s);
				pf.property.tab.setEnabled(true);
				pf.tab.setEnabled(true);
				MainFrame.getInstance().tabPan.setEnabled(true);
				dialog.dispose();
				contentChanged();
			}
		}
		// close warning dialog
		if(e.getSource().equals(noNameOk)){
			noName.dispose();
		}
	}
	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		contentChanged();
	}

	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource().equals(dialog)){
			int s=MainFrame.getInstance().tabPan.getSelectedIndex();
			PolicyFrame pf= MainFrame.getInstance().policies.get(s);
			pf.property.tab.setEnabled(true);
			pf.tab.setEnabled(true);
			MainFrame.getInstance().tabPan.setEnabled(true);
			dialog.dispose();
		}
	}

	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
